package com.sgcc.fj.pmos.e_fj_home.activity;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.arouter.home.HomeArouterPath;
import com.sgcc.fj.pmos.common.base.BaseRecyclerAdapter;
import com.sgcc.fj.pmos.common.base.MvpBaseActivity;
import com.sgcc.ln.pmos.common.widget.LoadingLayout;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.fj.pmos.e_fj_home.adapter.InforDiscloFirstStepAdapter;
import com.sgcc.fj.pmos.e_fj_home.adapter.InforDiscloSecondStepAdapter;
import com.sgcc.fj.pmos.e_fj_home.bean.InformationDisclosure2Bean;
import com.sgcc.fj.pmos.e_fj_home.bean.InformationDisclosureBean;
import com.sgcc.fj.pmos.e_fj_home.contract.InformationDisclosureContract;
import com.sgcc.fj.pmos.e_fj_home.presenter.InformationDisclosurePresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * 信息披露
 */
@Route(path = HomeArouterPath.HOME_INFORMATIONDISCLOSURE)
public class InformationDisclosureActivity extends MvpBaseActivity<InformationDisclosurePresenter> implements InformationDisclosureContract.View, LoadingLayout.RetryListener {

    private TitleBar mTitleBar;
    private LoadingLayout mLoadlayout;
    private RecyclerView mRecycleViewFirstStep;
    private RecyclerView mRecycleViewSecondStep;

    private InforDiscloSecondStepAdapter mInforDiscloSecondStepAdapter;
    private InforDiscloFirstStepAdapter mInforDiscloFirstStepAdapter;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_informationdisclosure;
    }

    @Override
    protected InformationDisclosurePresenter createPresenter() {
        return new InformationDisclosurePresenter();
    }

    @Override
    protected void initViews() {
        super.initViews();
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mLoadlayout = (LoadingLayout) findViewById(R.id.loadlayout);
        mRecycleViewFirstStep = (RecyclerView) findViewById(R.id.recycle_view_first_step);
        mRecycleViewSecondStep = (RecyclerView) findViewById(R.id.recycle_view_second_step);

        mTitleBar.setTitle("信息披露");
        mLoadlayout.setRetryListener(this);

        LinearLayoutManager secondLLManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecycleViewSecondStep.setLayoutManager(secondLLManager);
        mRecycleViewSecondStep.setItemAnimator(new DefaultItemAnimator());
        mInforDiscloSecondStepAdapter = new InforDiscloSecondStepAdapter(this);
        mRecycleViewSecondStep.setAdapter(mInforDiscloSecondStepAdapter);

        LinearLayoutManager firstLLManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecycleViewFirstStep.setLayoutManager(firstLLManager);
        mRecycleViewFirstStep.setItemAnimator(new DefaultItemAnimator());
        mInforDiscloFirstStepAdapter = new InforDiscloFirstStepAdapter();
        mInforDiscloFirstStepAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, Object data) {
                mInforDiscloFirstStepAdapter.setSelectPosition(position);
                if (position == 0) {
                    //首页单独处理
                    setSecondStepAdapterData();
                } else {
                    InformationDisclosureBean.DataBean dataBean = (InformationDisclosureBean.DataBean) data;
                    mPresenter.getMenu2List(dataBean.getGuid(), true);
                }
            }
        });
        mRecycleViewFirstStep.setAdapter(mInforDiscloFirstStepAdapter);
        mPresenter.getMenuList(true);

    }

    @Override
    public void onErrorclick() {
        mPresenter.getMenuList(true);
    }

    @Override
    public void getMenuListSuccess(InformationDisclosureBean response) {
        if (response.getData() != null && response.getData().size() > 0) {
            mLoadlayout.showContent();
            List<InformationDisclosureBean.DataBean> list = response.getData();
            mInforDiscloFirstStepAdapter.addDatas(list);
            setSecondStepAdapterData();
        } else {
            mLoadlayout.showError();
        }
    }

    @Override
    public void getMenuListFailed() {
        mLoadlayout.showEmpty();
    }

    /**
     * 首页单独处理
     * 给首页加数据   最新信息  最热信息
     */
    private void setSecondStepAdapterData(){
        List<InformationDisclosure2Bean.DataBean> list = new ArrayList<>();
        InformationDisclosure2Bean.DataBean bean = new InformationDisclosure2Bean.DataBean();
        bean.setLabel("最新信息");
        bean.setInfoCode("zxxx");
        bean.setChildren(new ArrayList<>());
        InformationDisclosure2Bean.DataBean bean1 = new InformationDisclosure2Bean.DataBean();
        bean1.setLabel("最热信息");
        bean1.setInfoCode("zrxx");
        bean1.setChildren(new ArrayList<>());
        list.add(bean);
        list.add(bean1);
        mInforDiscloSecondStepAdapter.addDatas(list);
    }

    @Override
    public void getMenu2ListSuccess(InformationDisclosure2Bean response) {
        if (response.getData() != null && response.getData().size() > 0) {
            mInforDiscloSecondStepAdapter.addDatas(response.getData().get(0).getChildren());
        }
    }

    @Override
    public void getMenu2ListFailed() {

    }
}
